//
//  EasyTabBar.m
//  TestDemo
//
//  Created by 张润东 on 2021/8/24.
//

#import "EasyTabBar.h"
#import "EasyTabBarBtn.h"

@interface EasyTabBar ()

@property(nonatomic,assign)NSInteger spIndex;
@property(nonatomic,assign)BOOL special;
@property(nonatomic,assign)CGFloat upperFloat;

@end

@implementation EasyTabBar


/// 初始化方法
/// @param normalImgs 普通图片  选中图片默认 %@__selected  请导入资源时遵守规则
/// @param special 是否支持突出向上显示
/// @param index 向上突出显示的index
/// @param upperFloat 向上偏移量
+ (instancetype)tabBarWithImgNames:(NSArray*)normalImgs special:(BOOL)special specialIndex:(NSInteger)index upperFloat:(CGFloat)upperFloat{
    EasyTabBar *tabBar = [[EasyTabBar alloc]init];
    tabBar.special =special;
    tabBar.spIndex=index;
    tabBar.upperFloat = upperFloat;
    //创建图片对应的按钮 -- tabBarItem
    for (NSString *normalImg in normalImgs) {
        EasyTabBarBtn *btn = [EasyTabBarBtn buttonWithType:UIButtonTypeCustom];
        //记录按钮的索引
        btn.tag = tabBar.subviews.count+10000;
        [tabBar addSubview:btn];
        //加载按钮中显示的图片
        NSString *imgName = [NSString stringWithFormat:@"%@",normalImg];
        NSString *selectedImgName = [NSString stringWithFormat:@"%@_selected",normalImg];
        //设置按钮的图片
        [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:selectedImgName] forState:UIControlStateSelected];
        [btn.imageView setContentMode:UIViewContentModeScaleAspectFit];
        //给按钮注册点击事件
        [btn addTarget:tabBar action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    }
    //让第一个按钮选中
    UIButton *btn = [tabBar.subviews firstObject];
    [tabBar btnClick:btn];
    return tabBar;
}
/// 点击事件
/// @param sender 被点击的按钮
- (void)btnClick:(UIButton *)sender {
    //选中状态直接返回
    if (sender.selected) {
        return;
    }
    //非选中状态点击事件
    sender.selected = YES;
    self.selectBtn = sender;
    if (self.preButton) {
        self.preButton.selected = NO;
    }
    //代理方法
    if (self.preButton && self.delegate && [self.delegate respondsToSelector:@selector(tabBarDidSelectIndex:disIndex:selectBtn:disBtn:)]) {
        [self.delegate tabBarDidSelectIndex:sender.tag-10000 disIndex:self.preButton.tag-10000 selectBtn:sender disBtn:self.preButton];
    }
    //重置完毕,上一个按钮要先指到当前按钮
    self.preButton = sender;
}

//layout设置按钮的位置
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat btnW = self.bounds.size.width / self.subviews.count;
    CGFloat btnX = 0;
    CGFloat btnH;
    CGFloat btnY;
    for (int i = 0; i < self.subviews.count; i++) {
        UIButton *btn = self.subviews[i];
        //设置按钮的位置
        if (i == self.spIndex && self.special) { // 中间不规则按钮位置计算
            btnY = -self.upperFloat;
            btnH = 65;
        } else { // 普通按钮位置计算
            btnY = 0;
            btnH = self.bounds.size.height;
        }
        btnX = i * btnW;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
    
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
